package integration.mdbs;

import integration.utils.ParseXML;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenBean;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.InitialContext;

import entities.CommonUtils;
import entities.vo.OFADVO;
import fachada.Fachada;

@MessageDriven(activationConfig = {
		@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Topic"),
		@ActivationConfigProperty(propertyName="destination", propertyValue="topic/OfADTopic3")/*,
		@ActivationConfigProperty(propertyName="providerAdapterJNDI", propertyValue="java:/RemoteJMSProvider3")*/

})

public class ProcesarPedidosBean3 implements MessageDrivenBean, MessageListener {

	protected Fachada 			fachada;
	private InitialContext 		initialContext;
	/**
	 * 
	 */
	private static final long serialVersionUID = -2046025147614693410L;

	public ProcesarPedidosBean3() {
		inicializarContexto();
	}

	private void inicializarContexto() {
		try{
			String naming = "TiendaZARAServidorEAR/FachadaBean/remote";
			Hashtable<String, String> props = new Hashtable<String, String>();
			props.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
			//Url completa de ubicacion del servidor de aplicaciones
			props.put(InitialContext.PROVIDER_URL,"jnp://"+CommonUtils.HOST+":1099");
			//props.put(InitialContext.PROVIDER_URL,"jnp://172.16.171.116:1099");
			//Objeto del tipo InitialContext
			initialContext = new InitialContext(props);
			fachada = (Fachada) initialContext.lookup(naming);
		}catch(Exception ex){
			System.out.println(ex.getMessage());
			ex.printStackTrace();
		}
	}
	public void ejbRemove() throws EJBException {
		// TODO Auto-generated method stub

	}

	public void setMessageDrivenContext(MessageDrivenContext arg0)
		throws EJBException {
		// TODO Auto-generated method stub

	}

	public void onMessage(Message arg0) {
		// TODO Auto-generated method stub
		TextMessage msg = null;
		
		try {
			if (arg0 instanceof TextMessage) {
				msg = (TextMessage) arg0;
				fachada.procesarNuevaOFAD(getOFAD(msg.getText()));
				System.out.println("MESSAGE BEAN: Mensaje recibido: "+ msg.getText() );
			} else {
				System.out.println("MESSAGE BEAN: Mensaje de tipo incorrecto ");
			}
				
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public OFADVO getOFAD(String ofadXML) throws FileNotFoundException, IOException {
        ParseXML parse = new ParseXML();
        return parse.getOFAD(ofadXML);
    }
	/**
	 * Default create method
	 * 
	 * @throws CreateException
	 */
	public void ejbCreate() throws CreateException {
		// TODO Auto-generated method stub
	}
}
